********************************************************************************
* ELECTORAL REPERCUSSIONS OF A PANDEMIC:  EVIDENCE FROM THE 2009 H1N1 OUTBREAK *
********************************************************************************

// NOVEMBER 2, 2021

// THIS FILE REPLICATES TABLES B1, B2, AND B3 IN APPENDIX B OF "ELECTORAL 
// REPERCUSSIONS OF A PANDEMIC: EVIDENCE FROM THE 2009 H1N1 OUTBREAK" BY
// EMILIO GUTIÉRREZ, JAAKKO MERILÄINEN, AND ADRIÁN RUBLI

global data "XXX" // Insert data directory here
global output "XXX" // Insert output directory here

* Open data

use "$data/GMR_data_main.dta", clear

egen munid=group(estado municipio) // Generate an identifier for municipalities
gen year2=year*year

********************************************************************************

* Table B1

est clear

eststo: reghdfe PAN_voteshare_president ib2006.year##c.change_ari [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

eststo: reghdfe PAN_voteshare_president ib2006.year##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"

esttab est*  using "$output/tableB1.tex", replace noomitted nobaselevels booktabs  mlabels(none) ///
	compress lines star(* .1 ** .05 *** .01) label ///
	b(%9.3f) se(%9.3f) stats(N1 N2 N3 district stateyear, labels("Number of observations" "Number of sections" "Number of clusters" "Section FE" "Municipality-year FE") ///
	fmt(0 0 0)) keep(2012.year#c.change_ari 2012.year#c.change_ari_z) ///
	coeflabels(2012.year#c.change_ari "1[Year = 2012] $\times$ Excess ARI cases (1000s)" ///
	2012.year#c.change_ari_z "1[Year = 2012] $\times$ Excess ARI cases (z-score)") ///
	nodepvars nomtitle prehead(\begin{tabular}{l*{3}{c}} \toprule) ///
	postfoot(\bottomrule \end{tabular})

est clear

* Table B2

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari!=. & PRI_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

est clear

eststo: reghdfe PRI_voteshare 1.post##c.change_ari [aw = total_votos] if year==2006 | year==2009, absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "
restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari_z!=. & PRI_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe PRI_voteshare 1.post##c.change_ari_z [aw = total_votos] if year==2006 | year==2009, absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "
restore

preserve

gen a=1 if change_ari!=. & PRI_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PRI_voteshare 1.post##c.change_ari [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari_z!=. & PRI_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PRI_voteshare 1.post##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari!=. & PRI_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PRI_voteshare 1.post##c.change_ari [aw = total_votos], absorb(id i.munid#i.year i.id#c.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear "\checkmark"
estadd local quadratic " "

restore


preserve

gen a=1 if change_ari_z!=. & PRI_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PRI_voteshare 1.post##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year i.id#c.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear "\checkmark"
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari!=. & PRI_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PRI_voteshare 1.post##c.change_ari [aw = total_votos], absorb(id i.munid#i.year i.id#c.year i.id#c.year2) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic "\checkmark"

restore

preserve

gen a=1 if change_ari_z!=. & PRI_voteshare!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe PRI_voteshare 1.post##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year i.id#c.year i.id#c.year2) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic "\checkmark"

restore

esttab est*  using "$output/tableB2.tex", replace noomitted nobaselevels booktabs  mlabels(none) ///
	compress lines star(* .1 ** .05 *** .01) label ///
	mgroups("2006-2009" "1997-2012", pattern(1 0 1 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	b(%9.3f) se(%9.3f) stats(N1 N2 N3 district stateyear linear quadratic, labels("Number of observations" "Number of sections" "Number of clusters" "Section FE" "Municipality-year FE" "Linear time trend" "Quadratic time trend") ///
	fmt(0 0 0)) keep(1.post#c.change_ari 1.post#c.change_ari_z) ///
	coeflabels(1.post#c.change_ari "1[Year $\geq$ 2009] $\times$ Excess ARI cases (1000s)" ///
	1.post#c.change_ari_z "1[Year $\geq$ 2009] $\times$ Excess ARI cases (z-score)") ///
	order(1.post#c.change_ari 1.post#c.change_ari_z) ///
	nodepvars nomtitle prehead(\begin{tabular}{l*{9}{c}} \toprule) ///
	postfoot(\bottomrule \end{tabular})
	
********************************************************************************
	
* Table B3

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari!=. & others!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

est clear

eststo: reghdfe others 1.post##c.change_ari [aw = total_votos] if year==2006 | year==2009, absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "
restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari_z!=. & others!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe others 1.post##c.change_ari_z [aw = total_votos] if year==2006 | year==2009, absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "
restore

preserve

gen a=1 if change_ari!=. & others!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe others 1.post##c.change_ari [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari_z!=. & others!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe others 1.post##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari!=. & others!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe others 1.post##c.change_ari [aw = total_votos], absorb(id i.munid#i.year i.id#c.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear "\checkmark"
estadd local quadratic " "

restore


preserve

gen a=1 if change_ari_z!=. & others!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe others 1.post##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year i.id#c.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear "\checkmark"
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari!=. & others!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe others 1.post##c.change_ari [aw = total_votos], absorb(id i.munid#i.year i.id#c.year i.id#c.year2) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic "\checkmark"

restore

preserve

gen a=1 if change_ari_z!=. & others!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==6

eststo: reghdfe others 1.post##c.change_ari_z [aw = total_votos], absorb(id i.munid#i.year i.id#c.year i.id#c.year2) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/6
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic "\checkmark"

restore

esttab est*  using "$output/tableB3.tex", replace noomitted nobaselevels booktabs  mlabels(none) ///
	compress lines star(* .1 ** .05 *** .01) label ///
	mgroups("2006-2009" "1997-2012", pattern(1 0 1 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	b(%9.3f) se(%9.3f) stats(N1 N2 N3 district stateyear linear quadratic, labels("Number of observations" "Number of sections" "Number of clusters" "Section FE" "Municipality-year FE" "Linear time trend" "Quadratic time trend") ///
	fmt(0 0 0)) keep(1.post#c.change_ari 1.post#c.change_ari_z) ///
	coeflabels(1.post#c.change_ari "1[Year $\geq$ 2009] $\times$ Excess ARI cases (1000s)" ///
	1.post#c.change_ari_z "1[Year $\geq$ 2009] $\times$ Excess ARI cases (z-score)") ///
	order(1.post#c.change_ari 1.post#c.change_ari_z) ///
	nodepvars nomtitle prehead(\begin{tabular}{l*{9}{c}} \toprule) ///
	postfoot(\bottomrule \end{tabular})
